var path = require('path')
  , fs = require('fs')
  , express = require('express')
  , bodyParser = require('body-parser')
  , serveStatic = require('serve-static')
  , serveIndex = require('serve-index')
  , _ = require('underscore');

var server = module.exports = express();
var ftlRenderer = require('ftl-renderer');

server.set('view engine', 'ftl');
server.set('views', path.join(__dirname, '..', 'views'));
ftlRenderer.setOption({
  viewRoot: path.join(process.cwd(), 'views'),
  sourceEncoding:'utf-8',
  locale:'zh_CN'
});
server.engine('ftl', function(path, locals, cb){
  var config = _(locals).omit('settings', '_locals', 'cache');
  ftlRenderer.render(path, config, function(html){
    cb(null, html)
  });
});

server.use(bodyParser.urlencoded({ extended: true }))
      .use(bodyParser.json());

require('../mocks/sensordata.js')(server);
require('../mocks/setting.js')(server);
require('../mocks/ajax.js')(server);
require('../mocks/alert.js')(server);
require('../mocks/anr.js')(server);
require('../mocks/framerate')(server);
require('../mocks/geo')(server);
require('../mocks/settings')(server);
require('../mocks/newweboverview.js')(server);
require('../mocks/user-analysis')(server);
require('../mocks/user-info')(server);

require('./ftl-helpers')(server,ftlRenderer)

//entrys
require('./exts/entry')(server)
//html
//require('./exts/freemarker')(server)
//backend mocks
require('./exts/mocks')(server)
//static
require('./exts/assets')(server)
//webpack
require('./exts/webpack')(server)
//iconfont
require('./exts/iconfonts')(server)
//index
server.get('/',function(req,res,next){
  fs.readFile(path.join(__dirname, 'index.html'),function(err,content){
    res.end(content.toString())
  })
})


server.use('/public',
  serveIndex(path.join(__dirname, '..', 'node_modules/fed-lib/docs'),{view:'details'}),
  serveStatic(path.join(__dirname, '..', 'node_modules/fed-lib/docs'))
);
